package x.zero.util;


/**
 * 通用返回对象
 */
public class CommonResult<T> {
    //操作成功
    public static final int SUCCESS = 200;
    //操作失败
    public static final int FAILED = 500;
    //参数校验失败
    public static final int VALIDATE_FAILED = 404;
    //未认证
    public static final int UNAUTHORIZED = 401;
    //未授权
    public static final int FORBIDDEN = 403;
    private int code;
    private String msg;
    private T data;

    public CommonResult() {
    }

    protected CommonResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    /**
     * 普通成功返回
     *
     * @param data 获取的数据
     */
    public static CommonResult success(Object data) {
//        this.code = SUCCESS;
//        this.msg = "操作成功";
//        this.data = data;
        return new CommonResult(SUCCESS,"操作成功",data);
    }

    /**
     * 普通成功返回
     */
    public static CommonResult success() {
//        this.code = SUCCESS;
//        this.msg = "操作成功";
//        this.data = "操作成功";
        return new CommonResult(SUCCESS,"操作成功","操作成功");
    }

    /**
     * 普通成功返回
     */
    public static CommonResult success(String msg, Object data) {
//        this.code = SUCCESS;
//        this.msg = msg;
//        this.data = data;
        return new CommonResult(SUCCESS,msg,data);
    }

    /**
     * 普通成功返回
     */
    public static CommonResult fail(int code) {
//        this.code = code;
        return new CommonResult(code,null,null);
    }

    /**
     * 普通失败提示信息
     */
    public static CommonResult failed() {
//        this.code = FAILED;
//        this.msg = "操作失败";
        return new CommonResult(FAILED,"操作失败",null);
    }

    public static CommonResult failed(String msg) {
//        this.code = FAILED;
//        this.msg = msg;
//        this.data = msg;
        return new CommonResult(FAILED,msg,msg);
    }

    public static CommonResult failed(Integer code, String message) {
//        this.code = code;
//        this.msg = message;
        return new CommonResult(code,message,null);
    }

    /**
     * 参数验证失败使用
     *
     * @param msg 错误信息
     */
    public static CommonResult validateFailed(String msg) {
//        this.code = VALIDATE_FAILED;
//        this.msg = msg;
        return new CommonResult(VALIDATE_FAILED,msg,null);
    }

    /**
     * 未登录时使用
     *
     * @param msg 错误信息
     */
    public static CommonResult unauthorized(String msg) {
//        this.code = UNAUTHORIZED;
//        this.msg = "暂未登录或token已经过期";
//        this.data = msg;
        return new CommonResult(UNAUTHORIZED,"暂未登录或token已经过期",msg);
    }

    /**
     * 未授权时使用
     *
     * @param msg 错误信息
     */
    public static CommonResult forbidden(String msg) {
//        this.code = FORBIDDEN;
//        this.msg = "没有相关权限";
//        this.data = msg;
        return new CommonResult(FORBIDDEN,"没有相关权限",msg);
    }

    /**
     * 参数验证失败使用
     *
     * @param result 错误信息
     */
//    public CommonResult validateFailed(BindingResult result) {
//        validateFailed(result.getFieldError().getDefaultMessage());
//        return new CommonResult(FAILED,"操作失败",null);
//    }

    /**
     * 普通失败提示信息
     */
    public static CommonResult paramFailed() {
//        this.code = FAILED;
//        this.msg = "参数失败";
        return new CommonResult(FAILED,"参数失败",null);
    }

    public static CommonResult paramFailed(String msg) {
//        this.code = FAILED;
//        this.msg = msg;
        return new CommonResult(FAILED,msg,null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getmsg() {
        return msg;
    }

    public void setmsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
